//
//  SHTableViewCell.m
//  youjian03
//
//  Created by 常琼 on 14/10/20.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "NSDate+MJ.h"
#import "UIImage+NJ.h"
#import "SHPingViewController.h"
#import "MLPlayVoiceButton.h"
#import "HMNavigationController.h"

@interface SHTableViewCell()

@property(nonatomic,assign) int voicetime;

@property (nonatomic,retain) NSTimer * playVoiceTimer;
@property (nonatomic,assign) NSInteger currentPlayTime;
@property (nonatomic,assign) NSInteger currentRecorderTime;


@property(nonatomic,weak) MLPlayVoiceButton *playVoiceTypeOneImage;//语音按钮

@end

@implementation SHTableViewCell

/**
 *  为model赋值
 */
-(void)setMyPostModel:(SHMyPostModel *)myPostModel
{
    if (myPostModel!=nil)
    {

        //设置公司名
        self.postname.text = myPostModel.companyname;
        
        //设置name
        self.name.text = myPostModel.name;
        
        //设置时间
        NSDate *timeoff =  [NSString longtotime:myPostModel.finishtime];
        self.time .text =  [self createdWithTime:timeoff];
        
        
        //语音时长
        self.voicetime = [myPostModel.duration intValue];
        
        
        //设置订单号
        self.orderid = myPostModel.deliverId;
        
        //是否已经回复(评价)
        self.iscomment = myPostModel.hascomment;
        
        if ([[NSString stringWithFormat:@"%@",myPostModel.hascomment] isEqualToString:@"0"])
        {
            
        }
        else
        {
            self.ping .enabled = NO;
        }
        //快递公司号码
        self.typeNo = myPostModel.typeNo;
    
        
        
        //设置语音按钮new
        MLPlayVoiceButton * voiceBtn = [[MLPlayVoiceButton alloc] init];
        voiceBtn.frame = CGRectMake(138 , 81, 42, 42);
        
        self.playVoiceTypeOneImage = voiceBtn;
        [self addSubview:self.playVoiceTypeOneImage];
        NSString * audioString = [[myPostModel.audio componentsSeparatedByString:@"/"] lastObject];
       
        [self.playVoiceTypeOneImage setVoiceWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@audio/%@?download",SHAPIURL_VOICE_B,audioString]]];
        
   
        //设置头像
        NSString *uu = [myPostModel .headico stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSRange range =  [uu rangeOfString:@"%0A"];
        if(range.length>0)//说明里边有%0A
        {
            // range.location
            NSRange range2 = {0,uu.length-3};
            uu = [uu substringWithRange:range2];
            
        }
   
        NSURL *url = [NSURL URLWithString:uu];

        [self.headview setImageWithURL:url  placeholderImage:[UIImage imageNamed:@"defaulthead"]];

        [self.headview.layer setCornerRadius:CGRectGetHeight([self.headview bounds]) / 2];
        self.headview.layer.masksToBounds = YES;
        
        //然后再给图层添加一个有色的边框，类似qq空间头像那样
        self.headview.layer.borderWidth = 1;
        self.headview.layer.borderColor = [[UIColor whiteColor] CGColor];
        
    
}
}

-(void)clickVoice
{
    [self.playVoiceTypeOneImage setVoiceWithURL:[NSURL URLWithString:self.myPostModel.audio]];
}
 /**
 *  返回"刚刚"
 */
- (NSString *)createdWithTime:(NSDate *)date
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    // 获得微博发布的具体时间
    NSDate *createDate = date;
    
    
    // 判断是否为今年
    if (createDate.isThisYear) {
        if (createDate.isToday) { // 今天
            NSDateComponents *cmps = [createDate deltaWithNow];
            if (cmps.hour >= 1) { // 至少是1小时前发的
                return [NSString stringWithFormat:@"%d小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1~59分钟之前发的
                return [NSString stringWithFormat:@"%d分钟前", cmps.minute];
            } else { // 1分钟内发的
                return @"刚刚";
            }
        } else if (createDate.isYesterday) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm";
            return [fmt stringFromDate:createDate];
        } else { // 至少是前天
            fmt.dateFormat = @"MM-dd HH:mm";
            return [fmt stringFromDate:createDate];
        }
    } else { // 非今年
        fmt.dateFormat = @"yyyy-MM-dd";
        return [fmt stringFromDate:createDate];
    }
}


- (void)awakeFromNib
{
    [self.ping addTarget:self action:@selector(ping:) forControlEvents:UIControlEventTouchUpInside];
}
/**
 *  点击评价按钮
 *
 */
-(void)ping:(UIButton *)button
{
    //TODO: 点击评价跳转到pingcontroller
    if ([self.delegate respondsToSelector:@selector(pingWithOrderId:andRow:)])
    {
        [ self.delegate pingWithOrderId:self.orderid andRow:self.row];
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    SHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell ==nil)
    {
        cell = [[SHTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.selected = NO;
    }
    
    return  cell;
}
/**
 *  跟踪订单
 */
- (IBAction)gen2:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(genWithCodeNo:andTypeNo:)])
    {
        [ self.delegate genWithCodeNo:self.orderid andTypeNo:self.typeNo];
        
    }
}


@end
